package com.cxyjy.travelguide.service.Impl;

import com.cxyjy.travelguide.entity.ExternalArtifact;
import com.cxyjy.travelguide.mapper.ExternalArtifactMapper;
import com.cxyjy.travelguide.service.ArtifactService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: jony
 * @Date : 2025/5/22 09:32
 * @VERSION v1.0
 */
@Slf4j
@Service("ArtifactService")
public class ArtifactServiceImpl implements ArtifactService {
    @Autowired
    ExternalArtifactMapper artifactMapper;

    @Override
    @Transactional
    public Map<String, Object> deleteArtifact(String objectCode) {
        Map<String, Object> resultMap = new HashMap<>();

        ExternalArtifact externalArtifact = artifactMapper.selectByCode(objectCode);
        if (externalArtifact == null) {
            resultMap.put("status", 10657); // 记录不存在
            return resultMap;
        }

        int i = artifactMapper.deleteByCode(objectCode);
        if (i == 0) {
            resultMap.put("status", 10657); // 删除失败
            throw new RuntimeException("删除文物失败: " + externalArtifact.getName()+"，code为："+objectCode);
        }

        log.info("删除文物成功，code为：{}，文物名为：{}", objectCode, externalArtifact.getName());
        resultMap.put("status", 200);
        return resultMap;
    }
}
